package com.qiaoanju.web.back.controller.system;

import com.qiaoanju.back.domain.system.UserModel;
import com.qiaoanju.back.service.system.IUserService;
import com.qiaoanju.commons.constants.SessionConstant;
import com.qiaoanju.commons.constants.StringConstant;
import com.qiaoanju.commons.enums.ResultCode;
import com.qiaoanju.web.controller.BaseController;
import com.qiaoanju.web.security.service.LoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zhangchong
 * on 2017/9/21.
 */
@Controller
@RequestMapping("/sysuser")
public class UserController extends BaseController {

    @Autowired
    private IUserService service ;

    @RequestMapping(value = "/index", method = { RequestMethod.GET })
    public String list(Model model, HttpServletRequest request,
                       HttpServletResponse response) throws Exception {
        //获取登录用户的信息
        model.addAttribute(SessionConstant.SESSION_USER, LoginUserService.getLoginUser());

        return "sys/user/list";

    }

    @RequestMapping(value = "/view", method = { RequestMethod.GET })
    public String view(Model model, HttpServletRequest request,
                       HttpServletResponse response) throws Exception {


        return "sys/user/view";

    }

    @RequestMapping(value = "/add", method = { RequestMethod.POST})
    public void insert(Model model, HttpServletRequest request,
                       HttpServletResponse response,UserModel user) throws Exception {


        //后台校验
        logger.info("***********************model:"+user);
        int result = service.insert(user);
        //处理结果
        if (result > 0){

            printJson(response, ResultCode.Success, StringConstant.INSERT_MSG);
        }else{
            printJson(response,ResultCode.Fail, StringConstant.OPERATE_FAILURE_MSG);
        }

    }

}